let App = getApp();
var util = require("../../utils/util.js")
Page({
  data: {
    // banner轮播组件属性
    indicatorDots: true, // 是否显示面板指示点	
    autoplay: true, // 是否自动切换
    interval: 3000, // 自动切换时间间隔
    duration: 800, // 滑动动画时长
    imgHeights: {}, // 图片的高度
    imgCurrent: {}, // 当前banne所在滑块指针

    // 页面元素
    bannerlist: [],
    items: {
     
    },
    newest: {},
    list: {},

    scrollTop: 0,
    insertTime:[]
  },

  onLoad: function() {
    // 设置页面标题
    App.setTitle();
    // 设置navbar标题、颜色
    App.setNavigationBar();
    // 获取首页数据
  
  },
  onShow:function(){
    this.getIndexData();
  },

  /**
   * 获取首页数据
   */
  getIndexData: function() {
    let _this = this;
    App._get('index/qiang', {}, function(result) {
      console.log("ss", result.data.list);
      _this.setData({list:result.data.list}); 
      _this.setData({ bannerlist: result.data.arr }); 
      
      _this.getTime();
    });
  },

  /**
   * 计算图片高度
   */
  imagesHeight: function(e) {
    // let imgId = e.target.dataset.id,
    //   itemKey = e.target.dataset.itemKey,
    //   ratio = e.detail.width / e.detail.height, // 宽高比
    //   viewHeight = 750 / ratio, // 计算的高度值
    //   imgHeights = this.data.imgHeights;

    // // 把每一张图片的对应的高度记录到数组里
    // if (typeof imgHeights[itemKey] === 'undefined') {
    //   imgHeights[itemKey] = {};
    // }
    // imgHeights[itemKey][imgId] = viewHeight;
    // // 第一种方式
    // let imgCurrent = this.data.imgCurrent;
    // if (typeof imgCurrent[itemKey] === 'undefined') {
    //   imgCurrent[itemKey] = Object.keys(this.data.items[itemKey].data)[0];
    // }
    // this.setData({
    //   imgHeights,
    //   imgCurrent
    // });
  },
  getTime:function(){
    var that = this;
    
    function nowTime() {//时间函数
      // console.log(a)
      var dates = that.data.list;
      for (var i = 0; i < dates.length; i++) {
        var etime = new Date(dates[i].grouendtime).getTime();
        
        var stime = new Date().getTime();
        //console.log("ss", stime,etime);
        var usedTime = etime - stime;

        dates[i].dat = usedTime/1000;
      }
      //console.log("dates", dates.length);
      for (var i = 0; i < dates.length; i++) {
       
        var intDiff = dates[i].dat  ;//获取数据中的时间戳
        //console.log("intDiff",intDiff)
        var day = 0, hour = 0, minute = 0, second = 0;
        if (intDiff > 0) {//转换时间
          day = Math.floor(intDiff / (60 * 60 * 24));
          hour = Math.floor(intDiff / (60 * 60)) - (day * 24);
          minute = Math.floor(intDiff / 60) - (day * 24 * 60) - (hour * 60);
          second = Math.floor(intDiff) - (day * 24 * 60 * 60) - (hour * 60 * 60) - (minute * 60);
          if (hour <= 9) hour = '0' + hour;
          if (minute <= 9) minute = '0' + minute;
          if (second <= 9) second = '0' + second;
          dates[i].dat = dates[i].dat-1;
          var str = hour + ':' + minute + ':' + second
         //  console.log(str)  
        } else {
          var str = "已结束！";
          clearInterval(timer);
        }
        // console.log(str);
        dates[i].endtimestr = str;//在数据中添加difftime参数名，把时间放进去
      }
      that.setData({
        list: dates
      })
      // console.log(that)
    }

    nowTime();
    var timer = setInterval(nowTime, 1000);
  },
  bindChange: function(e) {
    let itemKey = e.target.dataset.itemKey,
      imgCurrent = this.data.imgCurrent;
    // imgCurrent[itemKey] = e.detail.current;
    imgCurrent[itemKey] = e.detail.currentItemId;
    this.setData({
      imgCurrent
    });
  },

  goTop: function(t) {
    this.setData({
      scrollTop: 0
    });
  },

  scroll: function(t) {
    console.log("ss",t);
    this.setData({
      indexSearch: t.detail.scrollTop
    }), t.detail.scrollTop > 300 ? this.setData({
      floorstatus: !0
    }) : this.setData({
      floorstatus: !1
    });
  },

  onShareAppMessage: function() {
    return {
      title: "小程序首页",
      desc: "",
      path: "/pages/index/index"
    };
  }
});